package tech.aistar.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.font.DelegatingShape;
import tech.aistar.entity.*;
import tech.aistar.service.IAgentService;
import tech.aistar.service.ILandlordService;
import tech.aistar.service.IRegisterService;
import tech.aistar.service.ITenantService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private IRegisterService registerService;
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IAgentService agentService;
    @Autowired
    private ILandlordService landlordService;
    @RequestMapping("/check")
    public String Login(){

        return "/login/login";
    }

    @RequestMapping("/success")
    public String Home(HttpServletRequest request){
        String account = request.getParameter("account");
        Register register = registerService.selectByAccount(account);
        //System.out.println(register);
        request.getSession().setAttribute("register",register);
        String identity = register.getIdentity();
        if (identity.equals("1")) {
            Tenant tenant = tenantService.selectByregisterId(register.getId());
            request.getSession().setAttribute("tenant",tenant);

        }
       if (identity.equals("2")){
            Agent agent = agentService.selectByregisterId(register.getId());
            request.getSession().setAttribute("agent",agent);

        }
       if (identity.equals("3")){
            Landlord landlord = landlordService.selectByregisterId(register.getId());
            request.getSession().setAttribute("landlord",landlord);

        }

        return "/home_page/home";
    }

    @PostMapping("/selectaccount")
    public @ResponseBody Result select(String account,HttpServletRequest request) {
        Register register = registerService.selectByAccount(account);
        if (register != null) {
            request.setAttribute("register",register);
            return new Result("200", "修改成功");

        } else {
            return new Result("500", "修改失败");
        }
    }

    @PostMapping("/selectpassword")
    public @ResponseBody
    Result select(String account, String password) {
        Register register = registerService.selectPassword(account);

        if (password.equals(register.getPassword())) {
            return new Result("200", "密码正确");
        } else {
            return new Result("500", "密码错误");
        }
    }


}
